﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Data;
using System.Globalization;
using System.Windows;
using System.Windows.Media;

namespace IconEditorXBAP.IconElements
{
    [ValueConversion(typeof(bool), typeof(Visibility))]
    public class VisbilityConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return (bool)value ? Visibility.Visible : Visibility.Hidden;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new Exception("This conversion is not implemented!");
            //return (Visibility)value == Visibility.Visible;
        }
    }

    [ValueConversion(typeof(Color), typeof(SolidColorBrush))]
    public class ColorToFillConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {

            return new SolidColorBrush((Color)value);
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new Exception("This conversion is not implemented!");
            //return ((SolidColorBrush)value).Color;
        }
    }

    [ValueConversion(typeof(double[,]), typeof(double))]
    public class ExtentToWidthConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            double[,] extent = (double[,])value;
            return Math.Abs(extent[1, 0] - extent[0, 0]); // width
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new Exception("This conversion is not implemented!");
        }
    }

    [ValueConversion(typeof(double[,]), typeof(double))]
    public class ExtentToHeightConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            double[,] extent = (double[,])value;
            return Math.Abs(extent[1, 1] - extent[0, 1]); // height
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new Exception("This conversion is not implemented!");
        }
    }

    [ValueConversion(typeof(double[,]), typeof(double))]
    public class ExtentToCanvasLeftConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            double[,] extent = (double[,])value;
            return extent[0, 0] + 100; // left
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new Exception("This conversion is not implemented!");
        }
    }

    [ValueConversion(typeof(double[,]), typeof(double))]
    public class ExtentToCanvasTopConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            double[,] extent = (double[,])value;
            return extent[0, 1] + 100; // top
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new Exception("This conversion is not implemented!");
        }
    }

    [ValueConversion(typeof(PointCollection), typeof(PointCollection))]
    public class PointsConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            PointCollection pc = (PointCollection)value;
            PointCollection newpc = new PointCollection();

            foreach (Point p in pc)
            {
                newpc.Add(new Point(p.X + 100, p.Y + 100));
            }
            return newpc;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new Exception("This conversion is not implemented!");
        }
    }

    [ValueConversion(typeof(PointCollection), typeof(double))]
    public class PointsToCanvasTopConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            PointCollection pc = (PointCollection)value;
            double minY = double.MaxValue;
            for (int i = 0; i < pc.Count; i++)
            {
                if ((pc[i].Y + 100) < minY)
                {
                    minY = (pc[i].Y + 100);
                }
            }
            return minY;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new Exception("This conversion is not implemented!");
        }
    }

    [ValueConversion(typeof(PointCollection), typeof(double))]
    public class PointsToCanvasLeftConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            PointCollection pc = (PointCollection)value;
            double minX = double.MaxValue;
            for (int i = 0; i < pc.Count; i++)
            {
                if ((pc[i].X + 100) < minX)
                {
                    minX = (pc[i].X + 100);
                }
            }
            return minX;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new Exception("This conversion is not implemented!");
        }
    }

    //public class ParamsToExtentConverter : IMultiValueConverter
    //{
    //    public object Convert(object[] value, Type typeTarget, object param, CultureInfo culture)
    //    {
    //        double[,] extent = new double[2, 2];
    //        double left = (double)value[0]; //Canvas.GetLeft(this);
    //        double top = (double)value[1]; //Canvas.GetTop(this);
    //        double width = (double)value[2]; //this.Width;
    //        double height = (double)value[3]; //this.Height;

    //        extent[0, 0] = (left - 200) / 2;
    //        extent[0, 1] = (top - 200) / 2;
    //        extent[1, 0] = extent[0, 0] + width / 2;
    //        extent[1, 1] = extent[0, 1] + height / 2;

    //        return extent;
    //    }
    //    public object[] ConvertBack(object value, Type[] typeTarget,
    //                                object param, CultureInfo culture)
    //    {
    //        double[,] extent = (double[,])value;
    //        object[] values = new object[4];
    //        values[0] = extent[0, 0] * 2 + 200; // left
    //        values[1] = extent[0, 1] * 2 + 200; // top
    //        values[2] = 2 * Math.Abs(extent[1, 0] - extent[0, 0]); // width
    //        values[3] = 2 * Math.Abs(extent[1, 1] - extent[0, 1]); // height

    //        return values;
    //    }
    //}
}
